package com.zhz.mall.admin.modules.pms.controller;


import com.zhz.mall.admin.modules.pms.model.PmsProduct;
import com.zhz.mall.common.api.CommonPage;
import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.admin.dto.ProductConditionDTO;
import com.zhz.mall.admin.dto.ProductSaveParamsDTO;
import com.zhz.mall.admin.dto.ProductUpdateInitDTO;
import com.zhz.mall.admin.modules.pms.service.PmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author zhouhengzhe
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    PmsProductService productService;

    /**
     * 条件分页查询商品信息
     * url:'/product/list',
     * method:'get',
     * data:          axios 如果设置的是data属性就是以json的方式传递
     * params:{       axios 如果设置的是params属性就是以url参数的方式传递
     * 如果传递是URLSearchParams  会以formdata的方式传递
     * keyword: null,
     * pageNum: 1,
     * pageSize: 5,
     * publishStatus: null,
     * verifyStatus: null,
     * productSn: null,
     * productCategoryId: null,
     * brandId: null
     * };
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProduct>> list(ProductConditionDTO condition) {
        return CommonResult.success(CommonPage.restPage( productService.list(condition)));
    }

    /**
     * url:'/product/update/deleteStatus',
     * method:'post',
     * params:params
     */
    @PostMapping("/update/deleteStatus")
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        return productService.removeByIds(ids) ? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     * 更新是否新品状态
     * export function updateNewStatus(params) {
     * return request({
     * url:'/product/update/newStatus',
     * method:'post',
     * params:params
     * })
     * }
     */

    @PostMapping("/update/newStatus")
    public CommonResult updateNewStatus(@RequestParam("ids") List<Long> ids,
                                        @RequestParam("newStatus") Integer newStatus) {
        return productService.updateStatus(newStatus, ids, PmsProduct::getNewStatus) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 更新是否推荐
     * export function updateRecommendStatus(params) {
     * return request({
     * url:'/product/update/recommendStatus',
     * method:'post',
     * params:params
     * })
     * }
     */

    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                                              @RequestParam("recommendStatus") Integer recommendStatus) {
        return productService.updateStatus(recommendStatus, ids, PmsProduct::getRecommandStatus) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 更新是否上架
     * export function updatePublishStatus(params) {
     * return request({
     * url:'/product/update/publishStatus',
     * method:'post',
     * params:params
     * })
     * }
     */
    @PostMapping("/update/publishStatus")
    public CommonResult updatePublishStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("publishStatus") Integer publishStatus) {
        return productService.updateStatus(publishStatus, ids, PmsProduct::getPublishStatus) ? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     * 商品添加
     * url:'/product/create',
     * method:'post',
     * data:data    json
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody ProductSaveParamsDTO productSaveParamsDTO) {
        return productService.create(productSaveParamsDTO) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 获取编辑状态下商品信息
     * url:'/product/updateInfo/'+id,
     * method:'get',
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult getUpdateInfo(@PathVariable Long id) {
        ProductUpdateInitDTO updateInitDTO = productService.getUpdateInfo(id);
        return CommonResult.success(updateInitDTO);
    }


    /**
     * 商品修改—保存
     * url:'/product/update/'+id,
     * method:'post',
     * data:data   json
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@RequestBody @Valid ProductSaveParamsDTO productSaveParamsDTO) {
        return productService.update(productSaveParamsDTO) ? CommonResult.success(true) : CommonResult.failed();
    }
}

